package com.mocha.nezha.graph.example;

import com.mocha.nezha.nezha.PoleEdge;
import com.mocha.nezha.nezha.PoleVertex;
import org.jgrapht.Graph;
import org.jgrapht.alg.shortestpath.BFSShortestPath;
import org.jgrapht.graph.SimpleGraph;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class JGraphTExample {

    public static void main(String[] args) throws IOException {
        // 创建一个无向图
        Graph<TestVertex, PoleEdge> graph = new SimpleGraph<>(PoleEdge.class);

       TestVertex a =  new TestVertex("A");
       TestVertex b =  new TestVertex("B");
       TestVertex c =  new TestVertex("C");
       TestVertex d =  new TestVertex("D");
       TestVertex e =  new TestVertex("E");
       TestVertex f =  new TestVertex("F");
       TestVertex g =  new TestVertex("G");
        // 添加节点和边
        graph.addVertex(a);
        graph.addVertex(b);
        graph.addVertex(c);
        graph.addVertex(d);
        graph.addVertex(e);
        graph.addVertex(f);
        graph.addVertex(g);
        graph.addEdge(f, a, new PoleEdge(new ArrayList<>(), "f", "a", 1));
        graph.addEdge(a, b, new PoleEdge(new ArrayList<>(), "a", "b", 1));
        graph.addEdge(a, g, new PoleEdge(new ArrayList<>(), "a", "g", 1));
        graph.addEdge(a, c, new PoleEdge(new ArrayList<>(), "a", "c", 1));
        graph.addEdge(b, f, new PoleEdge(new ArrayList<>(), "b", "f", 1));
        graph.addEdge(c, f, new PoleEdge(new ArrayList<>(), "c", "f", 1));
        graph.addEdge(c, e, new PoleEdge(new ArrayList<>(), "c", "e", 1));
        graph.addEdge(d, e, new PoleEdge(new ArrayList<>(), "d", "e", 1));



        List<TestVertex> vertexList = findAllPaths(a, e, graph);
        List<PoleEdge> edgeList = findEdgePaths(a, e, graph);

        int size = vertexList.size();
        int j = 0;
        for (int i = 0; i < size - 1; i++) {
            TestVertex from = vertexList.get(i);
            TestVertex to = vertexList.get(i + 1);
            PoleEdge edge = edgeList.get(j);
            if (edge.findSource().equals(from)){
                System.out.println("ssss");
            }else {
                System.out.println("nnnnn");
            }

            System.out.println("FROM : "+ from.getX() + " TO: "+to.getX() +"  edge: "+edge.toString());
            j++;
        }



    }

    //最短距离；
    private static List<TestVertex> findAllPaths(TestVertex start, TestVertex end, Graph<TestVertex, PoleEdge> graph) {
        BFSShortestPath<TestVertex, PoleEdge> alg = new BFSShortestPath<>(graph);
        List<TestVertex> allGraphPaths = alg.getPath(start, end).getVertexList();
        return allGraphPaths;
    }


    public static List<PoleEdge> findEdgePaths(TestVertex start, TestVertex end, Graph<TestVertex, PoleEdge> graph) {
        BFSShortestPath<TestVertex, PoleEdge> alg = new BFSShortestPath<>(graph);
        return alg.getPath(start, end).getEdgeList();
    }


}